package com.ssy.lingxi.platform.manage.serviceimpl.paas;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.api.model.dto.countryarea.CountryAreaPAASDTO;
import com.ssy.lingxi.platform.manage.entity.base.CountryArea;
import com.ssy.lingxi.platform.manage.repository.CountryAreaRepository;
import com.ssy.lingxi.platform.manage.service.paas.ICountryAreaPAASService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @menu 国家 - （系统初始化）PAAS接口 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2020/7/20
 */
@Service
public class CountryAreaPAASServiceImpl implements ICountryAreaPAASService {

    @Resource
    private CountryAreaRepository countryAreaRepository;

    @Override
    @Transactional
    public Wrapper<Void> add(CountryAreaPAASDTO dto) {
        CountryArea countryArea = new CountryArea();
        BeanUtils.copyProperties(dto, countryArea);
        countryAreaRepository.saveAndFlush(countryArea);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> update(CountryAreaPAASDTO dto) {
        CountryArea countryArea = countryAreaRepository.findById(dto.getId()).orElse(null);
        if(countryArea == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        BeanUtils.copyProperties(dto, countryArea);
        countryAreaRepository.saveAndFlush(countryArea);
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> delete(CountryAreaPAASDTO request) {
        countryAreaRepository.deleteAllByIdIn(request.getIds());
        return Wrapper.success();
    }

    @Override
    @Transactional
    public Wrapper<Void> enable(CountryAreaPAASDTO request) {
        CountryArea countryArea = countryAreaRepository.findById(request.getId()).orElse(null);
        if(countryArea == null) {
            throw new BusinessException(ResponseCode.MAN_RECORDS_DON_T_EXIST);
        }
        countryArea.setState(request.getState());
        countryAreaRepository.saveAndFlush(countryArea);
        return Wrapper.success();
    }
}
